package mesweb.mesweb.service.impl.produce;

import cn.hutool.core.date.DateUnit;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.farvision.common.utils.WorkDayCalculateUtils;
import mesweb.mesweb.entity.produce.ProduceCalendar;
import mesweb.mesweb.mapper.produce.ProduceCalendarMapper;
import mesweb.mesweb.service.produce.IProduceCalendarService;
import mesweb.mesweb.vo.produce.ProduceCalendarVo;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @Description: 生产日历
 * @Author: jeecg-boot
 * @Date:   2021-08-21
 * @Version: V1.0
 */
@Service
public class ProduceCalendarServiceImpl extends ServiceImpl<ProduceCalendarMapper, ProduceCalendar> implements IProduceCalendarService {

    @Autowired
    private ProduceCalendarMapper produceCalendarMapper;
    @Override
    public IPage<ProduceCalendarVo> getProduceCalendars(Page page, QueryWrapper queryWrapper) {
        return produceCalendarMapper.getProduceCalendars(page,queryWrapper);
    }

    @Override
    public List<ProduceCalendarVo> getProduceCalendars(QueryWrapper queryWrapper) {
        return produceCalendarMapper.getProduceCalendars(queryWrapper);

    }
    @Override
    public Date getDate(Date StartDate, float duration, DateUnit dateUnit,Long  produceCalendarId) {

        //获得当前工作日历中所有的例外工作日和休息日
        if(produceCalendarId > 0) {
            ProduceCalendar produceCalendar =  produceCalendarMapper.selectById(produceCalendarId);
            if( null != produceCalendar )
            {
                String[] arrWorks = {},arrRests = {};
                String specialWorkDays =  produceCalendar.getSpecialWorkDays();
                if(StringUtils.isNotBlank(specialWorkDays))
                {
                     arrWorks = specialWorkDays.split("\\,");
                }
                String specialRestDays =  produceCalendar.getSpecialRestDays();
                if(StringUtils.isNotBlank(specialRestDays))
                {
                    arrRests = specialRestDays.split("\\,");
                }
                WorkDayCalculateUtils.initSpecialDays(Arrays.asList(arrWorks),Arrays.asList(arrRests));

                Float workHoursPerDay = 0F;
                //如果是自定义工作小时则取周一的工作时间？需要改成配置参数
                if(produceCalendar.isCustomed())
                {
                    workHoursPerDay = produceCalendar.getMonday();
                }
                else{
                    workHoursPerDay = produceCalendar.getWorkHours();
                }
                Date endDate = WorkDayCalculateUtils.getDate(StartDate,duration,DateUnit.HOUR,workHoursPerDay);

                return endDate;
            }
        }
        return null;
    }

    @Override
    public Float getMonthWorkhours(Date date, Long produceCalendarId) {
        Float result = 0F;
        if (produceCalendarId > 0) {
            ProduceCalendar produceCalendar = produceCalendarMapper.selectById(produceCalendarId);
            if (null != produceCalendar) {
                String[] arrWorks = {}, arrRests = {};
                String specialWorkDays = produceCalendar.getSpecialWorkDays();
                if (StringUtils.isNotBlank(specialWorkDays)) {
                    arrWorks = specialWorkDays.split("\\,");
                }
                String specialRestDays = produceCalendar.getSpecialRestDays();
                if (StringUtils.isNotBlank(specialRestDays)) {
                    arrRests = specialRestDays.split("\\,");
                }
                WorkDayCalculateUtils.initSpecialDays(Arrays.asList(arrWorks), Arrays.asList(arrRests));
                Date startDate = new Date(date.getYear(), date.getMonth(), 1);
                Date newDate = DateUtils.addMonths(date, 1);
                Date endDate = new Date(newDate.getYear(), newDate.getMonth(), 1);
                result = WorkDayCalculateUtils.getWorkdays(startDate, endDate);

                //如果是自定义工作小时则取周一的工作时间？需要改成配置参数
                if(produceCalendar.isCustomed())
                {
                    result *= produceCalendar.getMonday();
                }
                else{
                    result *= produceCalendar.getWorkHours();
                }
            }
        }
        return result;
    }
}
